package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ProviderInfo;
import ywpt.web.common.PublicFunction;

public class ProviderServiceImpl extends HibernateEntityDao<ProviderInfo> implements ProviderService {

	@Override
	public String createProviderCode() {
		DetachedCriteria query = DetachedCriteria.forClass(ProviderInfo.class, "provider");
		String providerCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<ProviderInfo> providerInfos = this.getProviderInfos(query);
		if ( providerInfos.iterator().hasNext() )	
		{
			code = providerInfos.iterator().next().getCode();
			maxId = new Integer(code);
		}
		providerCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return providerCode;
	}

	@SuppressWarnings("unchecked")
	private List<ProviderInfo> getProviderInfos(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@Override
	public ProviderInfo getProviderInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public int getProviderInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProviderInfo> getProviderInfos(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<ProviderInfo> getProviderInfos() {
		return this.getAll();
	}

	@Override
	public void removeProviderInfo(Long id) {
		this.removeById(id);
	}

	@Override
	public void saveProviderInfo(ProviderInfo providerInfo) {
		this.save(providerInfo);
	}

	@Override
	public void updateProviderInfo(ProviderInfo providerInfo) {
		this.update(providerInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProviderInfo> getProvidersByProviderName(String providerName,
			int start, int size) {
		DetachedCriteria query = DetachedCriteria.forClass(ProviderInfo.class, "provider");
		//query.add(Restrictions.eq("organType", "01"));
		if ( providerName != null && !providerName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("name", "%" + providerName + "%"),  
							          Restrictions.like("code", "%" + providerName + "%")));
				// .add(Restrictions.eq("organType", "01"));
		}
		query.addOrder(Order.asc("code"));
		List<ProviderInfo> providerList = new ArrayList<ProviderInfo>();
		List<ProviderInfo> providerLists = new ArrayList<ProviderInfo>();
		ProviderInfo provider =new ProviderInfo();
		
		providerList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<ProviderInfo> itr = providerList.iterator(); itr.hasNext(); i++ ) 
		{
			provider = itr.next();
			String ss = PublicFunction.format(provider.getCode() + "",5, "  ", 1)
			          + provider.getName(); 
			
			provider.setName(ss);
			providerLists.add(provider);
		}
		return providerLists;
	}

	@Override
	public int getProvidersCountByName(String providerName) {
		DetachedCriteria query = DetachedCriteria.forClass(ProviderInfo.class, "privider");
		if ( providerName != null && !providerName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("name", "%" + providerName + "%" ),  
							          Restrictions.like("code", "%" + providerName + "%" )));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

	@Override
	public String getProviderInformationById(Long id) {
		String information = "";
		ProviderInfo providerInfo = new ProviderInfo();
		providerInfo = this.get(id);
		information = "银行及账号："+providerInfo.getBank()+" ,联系人："+providerInfo.getLinkMan()+",电话："+providerInfo.getPhone()+",地址："+providerInfo.getAddress();
		return information;
	}
}
